﻿<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head><link rel="stylesheet" href="${ctx}/common/common.css">
    <title>Title</title>
    <link rel="stylesheet" href="${ctx}/common/plugins/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctx}/common/plugins/layuiadmin/style/admin.css" media="all">
    <script src="${ctx}/common/plugins/layuiadmin/layui/layui.js"></script>
    
</head>
<body>
<form class="layui-form" action="${ctx}/role/${action}" style="margin-top: 50px">
    <input type="hidden" name="id" id="roleId" value="${entity.id}" />
    <input type="hidden" id="roleName" value="${entity.name}" />
    <input type="hidden" id="roleCode" value="${entity.code}" />
    <div class="layui-form-item layui-col-md5">
        <label class="layui-form-label">角色名称<span style="color: red">*</span></label>
        <div class="layui-input-block">
            <input type="text" name="name" required value="${entity.name}"  lay-verify="isRepeat" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-col-md5">
        <label class="layui-form-label">角色编码<span style="color: red">*</span></label>
        <div class="layui-input-block">
            <input type="text" name="code" lay-verify="isRepeatCode" value="${entity.code}"  placeholder="请输入编码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <%-- <div class="layui-form-item">
        <label class="layui-form-label">数据权限<span style="color: red">*</span></label>
        <div class="layui-input-block">
            <input type="radio" name="dataPermissions" ${entity.dataPermissions eq '0'?'checked':true} value="0" title="全部">
            <input type="radio" name="dataPermissions" ${entity.dataPermissions eq '1'?'checked':true} value="1" title="本部门" <c:if test="${empty entity.dataPermissions}">checked="checked"</c:if> >
            <input type="radio" name="dataPermissions" ${entity.dataPermissions eq '2'?'checked':true} value="2" title="自己">
        </div>
    </div> --%>
    <div class="layui-form-item layui-col-md5">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <input type="text" name="described" value="${entity.described}" placeholder="请输入描述" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary" lay-filter="reset">重置</button>
            <button type="button" id="rollback" class="layui-btn layui-btn-primary">返回</button>
        </div>
    </div>
</form>

<script>
    //Demo
    layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
        	layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $(this).attr("disabled",true);
        });
    });
    layui.use('form', function(){
    	var $ = layui.$;
    	var form = layui.form;
    	 form.verify({
             isRepeat: function(value, item){ //value：表单的值、item：表单的DOM对象
                 if(value.trim().length<=0){
                     return "必填项不能为空";
                 }
                 console.log($("#roleName").val());
                 if($("#roleName").val() != value){
 	                var isSubmit=true;
 	                $.ajax({
 	                    url: "${ctx}/role/checkName",
 	                    data: {checkName:value}, //请求的附加参数，用json对象
 	                    method: 'POST',
 	                    async:false,
 	                    success: function (data) {
 	                        if (data == '0') {
 	                            isSubmit=false;
 	                        }
 	                    }
 	                });
 	                if(isSubmit==false){
 	                    return "角色名称已存在";
 	                }
                 }
             },
             isRepeatCode: function(value, item){ //value：表单的值、item：表单的DOM对象
                 if(value.trim().length<=0){
                     return "必填项不能为空";
                 }
                 var reg = /^[0-9a-zA-Z]*$/
               	 if(value.trim().length>0){
               		 if (!reg.test(value)) {
               			 return "只能填写数字或字母";
               		 }
                 }
                 console.log($("#roleCode").val());
                 if($("#roleCode").val() != value){
 	                var isSubmit=true;
 	                $.ajax({
 	                    url: "${ctx}/role/checkCode",
 	                    data: {checkCode:value}, //请求的附加参数，用json对象
 	                    method: 'POST',
 	                    async:false,
 	                    success: function (data) {
 	                        if (data == '0') {
 	                            isSubmit=false;
 	                        }
 	                    }
 	                });
 	                if(isSubmit==false){
 	                    return "角色编码已存在";
 	                }
                 }
             }
         });
    });
    document.getElementById("rollback").addEventListener("click",function(){
        window.history.back();
    })
</script>
</body>
</html>



